package cn.shyouchi.wuguanjia.android.widget.dialog;

import android.app.Activity;
import android.app.DialogFragment;
import android.app.FragmentTransaction;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.Global;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.model.User;
import cn.shyouchi.wuguanjia.android.model.UserData;
import cn.shyouchi.wuguanjia.android.utils.LogUtil;

/**
 * Created by movier on 9/19/16.
 */
public class ConfirmIdInfoDialogFragment extends DialogFragment {

    @BindView(R.id.id_info_head_image_view)
    ImageView headImageView;
    @BindView(R.id.confirm_id_info_name)
    TextView nameTextView;
    @BindView(R.id.confirm_id_info_gender)
    TextView genderTextView;
    @BindView(R.id.confirm_id_info_address)
    TextView addressTextView;
    @BindView(R.id.confirm_id_info_authority)
    TextView authorityTextView;
    @BindView(R.id.confirm_id_info_birthday)
    TextView birthdayTextView;
    @BindView(R.id.confirm_id_info_number)
    TextView numberTextView;
    @BindView(R.id.confirm_id_info_people)
    TextView peopleTextView;
    @BindView(R.id.confirm_id_info_cancel_button)
    Button btnCancel;
    @BindView(R.id.confirm_id_info_next_button)
    Button btnEnter;


    @BindView(R.id.confirm_id_info_validate)
    TextView validateTextView;

    User mUser;
    UserData mUserData;


    private static final String TAG = "ConfirmIdInfo";

    public interface ConfirmIdInfoDialogListener {
        void onDialogPositiveClick(DialogFragment dialog);

        void onDialogNegativeClick(DialogFragment dialog);
    }

    // Use this instance of the interface to deliver action events
    ConfirmIdInfoDialogListener mListener;

    // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the host
            mListener = (ConfirmIdInfoDialogListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement NoticeDialogListener");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.dialog_fragment_confirm_id_info, container, false);
        ButterKnife.bind(this, view);


        if (mUserData != null) {

            LogUtil.json(mUserData);


            nameTextView.setText(mUserData.getName());
            genderTextView.setText(mUserData.getGender());
//            peopleTextView.setText(mUserData.getp());
            birthdayTextView.setText(mUserData.getBirthday());
            addressTextView.setText(mUserData.getAddress());
            numberTextView.setText(mUserData.getNumber());
            authorityTextView.setText(mUserData.getPublisher());

            btnCancel.setVisibility(View.GONE);
            btnEnter.setText("我了解了");

            validateTextView.setText(mUserData.getValidate());
            Glide.with(this)
                    .load(Global.BASE_URL + mUserData.getId_card_head_image())
                    .asBitmap()
                    .centerCrop()
                    .into(new BitmapImageViewTarget(headImageView) {
                        @Override
                        protected void setResource(Bitmap resource) {
                            RoundedBitmapDrawable circularBitmapDrawable =
                                    RoundedBitmapDrawableFactory.create(((Activity) mListener).getResources(), resource);
                            circularBitmapDrawable.setCircular(true);
                            headImageView.setImageDrawable(circularBitmapDrawable);
                        }
                    });


        } else {
            nameTextView.setText(mUser.getName());
            genderTextView.setText(mUser.getGender());
            peopleTextView.setText(mUser.getPeople());
            birthdayTextView.setText(mUser.getBirthday());
            addressTextView.setText(mUser.getAddress());
            numberTextView.setText(mUser.getNumber());
            authorityTextView.setText(mUser.getAuthority());
            validateTextView.setText(mUser.getValidate());
            Glide.with(this)
                    .load(new File(mUser.getIdCardHeadImagePath()))
                    .asBitmap()
                    .centerCrop()
                    .into(new BitmapImageViewTarget(headImageView) {
                        @Override
                        protected void setResource(Bitmap resource) {
                            RoundedBitmapDrawable circularBitmapDrawable =
                                    RoundedBitmapDrawableFactory.create(((Activity) mListener).getResources(), resource);
                            circularBitmapDrawable.setCircular(true);
                            headImageView.setImageDrawable(circularBitmapDrawable);
                        }
                    });

        }


        return view;
    }

    @OnClick(R.id.confirm_id_info_next_button)
    void nextButtonTapped() {
        Log.d(TAG, "nextButtonTapped");
        mListener.onDialogPositiveClick(this);

        dismiss();
    }


    public int show(FragmentTransaction transaction, String tag, User user) {
        mUser = user;
        if (mUser != null) {
            return super.show(transaction, tag);
        } else {
            return -1;
        }
    }

    public int show(FragmentTransaction transaction, String tag, UserData user) {
        mUserData = user;
        if (mUserData != null) {
            return super.show(transaction, tag);
        } else {
            return -1;
        }
    }


}
